//
//  BSNewFeatureViewController.swift
//  mySwiftWeibo
//
//  Created by apple on 16/7/24.
//  Copyright © 2016年 itcast沈庆伟. All rights reserved.
//

import UIKit

private let reuseIdentifier = "NewFeatureCell"

///collectionview cell的数量
private let ItemCount=4


class BSNewFeatureViewController: UICollectionViewController {

    ///layout
    let layout = UICollectionViewFlowLayout()
    
    ///在这个构造函数里给collectionview初始化一个layout
    init(){
        super.init(collectionViewLayout: layout)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Register cell classes
        self.collectionView!.registerClass(BSNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
        setupLayout()
    }
    
    ///设置流水布局
    private func setupLayout(){
        layout.itemSize=self.view.frame.size
        
        layout.minimumInteritemSpacing=0
        layout.minimumLineSpacing=0
        
        layout.scrollDirection=UICollectionViewScrollDirection.Horizontal
        
        collectionView?.pagingEnabled=true
        collectionView?.bounces=false
    }

}

//MARK: - 数据源方法
extension BSNewFeatureViewController{
    
    ///返回cell数量
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return ItemCount
    }
    
    ///返回cell
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        //根据identifier去缓存池找cell,如果找不到dequeue这个方法会根据注册的cell去创建一个新的cell
        let cell=collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! BSNewFeatureCell
        cell.backgroundColor=UIColor.randomColor()
        
        cell.image=UIImage(named: "new_feature_\(indexPath.item + 1)")
        
        if indexPath.item != ItemCount-1{
            cell.showButtons(false)
        }else{
            cell.showButtons(true)
        }
        
        return cell
    }
    
    
}